package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * @author atguigu-mqx
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    //  注入mapper
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  查询判断！一个订单对应的一种支付方式只能有一条记录！
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderInfo.getId());
        paymentInfoQueryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);

        if (paymentInfoQuery!=null){
            return;
        }
        //  声明一个对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //  给对象赋值！ 根据orderInfo 中的数据赋值！
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        //paymentInfo.setSubject("test");
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setUserId(orderInfo.getUserId());

        //  插入数据：
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type",paymentType);
        //  返回数据
        return paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
    }

    @Override
    public void paySuccess(String outTradeNo, String paymentType, HashMap<String, String> paramMap) {
        //  获取订单订单Id
        PaymentInfo paymentInfo = this.getPaymentInfo(outTradeNo, paymentType);

        //  需要更新数据：trade_no payment_status callback_time callback_content
        PaymentInfo paymentInfoUpd = new PaymentInfo();
        //  设置支付宝的交易号！
        paymentInfoUpd.setTradeNo(paramMap.get("trade_no"));
        paymentInfoUpd.setCallbackTime(new Date());
        paymentInfoUpd.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfoUpd.setCallbackContent(paramMap.toString());

        //  为了后续方法重用！
        this.updatePaymentInfo(outTradeNo,paymentType,paymentInfoUpd);

        //  发送 消息更改订单状态！
        //  发送的消息需要根据消费者业务决定！
        this.rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());
    }
    //  更新方法！
    public void updatePaymentInfo(String outTradeNo, String paymentType, PaymentInfo paymentInfoUpd) {
        //  设置条件：
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type",paymentType);
        paymentInfoMapper.update(paymentInfoUpd,paymentInfoQueryWrapper);
    }

    @Override
    public void closePayment(Long orderId) {
        //  设置更新条件！
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderId);

        //  细节处理：
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        if (paymentInfo==null) return;

        //  执行更新语句！
        PaymentInfo paymentInfoUpd = new PaymentInfo();
        paymentInfoUpd.setPaymentStatus(PaymentStatus.CLOSED.name());
        paymentInfoMapper.update(paymentInfoUpd,paymentInfoQueryWrapper);
    }
}
